library(mirt)
library(tidyverse)

CFAdata_3<-subset(limited, 
                  Obedient >-1 & Respect >-1 & GoodMannered >-1 & WellBehaved >-1 & Polite>-1 &
                    Orderly>-1 &Disciplined>-1 &Loyal>-1)



vars <- c("Respect", "Obedient", "GoodMannered", "WellBehaved",
          "Polite", "Orderly", "Disciplined", "Loyal")


irt_data_3 <- subset(CFAdata_3, select = vars)

group_3<-as.character(CFAdata_3$treatment )

table(group_3)
group_3 <- dplyr::recode(group_3, "1"="Control", "2"="Girl", "3"="Boy")

mod_configural_3<-multipleGroup( data=irt_data_3, 
                                 model=1, 
                                 group= group_3 ) 

mod_configural_3
summary(mod_configural_3)
M2(mod_configural_3)


mod_metricb_3<-multipleGroup( data=irt_data_3, 
                              model=1, 
                              group= group_3, 
                              invariance=c('intercepts'))

mod_metricb_3
summary(mod_metricb_3)
M2(mod_metricb_3)


anova( mod_metricb_3, mod_configural_3)

mod_metric_3<-multipleGroup( data=irt_data_3, 
                             model=1, 
                             group= group_3, 
                             invariance=c('slopes'))

mod_metric_3
summary(mod_metric_3)
M2(mod_metric_3)


anova( mod_metric_3, mod_configural_3)




mod_scalar2_3<-multipleGroup( data=irt_data_3, 
                              model=1, 
                              group= group_3, 
                              invariance=c('slopes', 'intercepts', 'free_var','free_means')
)



mod_scalar2_3
summary(mod_scalar2_3)
M2(mod_scalar2_3)

anova( mod_scalar2_3, mod_configural_3)

mod_scalar1_3<-multipleGroup( data=irt_data_3, 
                              model=1, 
                              group= group_3, 
                              invariance=c('slopes', 'intercepts', 'free_var')
)



mod_scalar1_3
summary(mod_scalar1_3)
M2(mod_scalar1_3)

anova( mod_scalar1_3, mod_configural_3)

mod_fullconstrain_3<-multipleGroup( data=irt_data_3, 
                                    model=1, 
                                    group= group_3, 
                                    invariance=c('slopes', 'intercepts')
)



mod_fullconstrain_3
summary(mod_fullconstrain_3)
M2(mod_fullconstrain_3)

anova( mod_fullconstrain_3, mod_configural_3)


anova(mod_metric_3, mod_configural_3) #equal slopes only
anova(mod_scalar2_3, mod_metric_3) #equal intercepts, free variance and mean
anova(mod_scalar1_3, mod_scalar2_3) #fix mean
anova(mod_fullconstrain_3, mod_scalar1_3) #fix variance


anova(mod_fullconstrain_3, mod_scalar1_3, mod_scalar2_3, mod_metric_3, mod_configural_3)


summary(mod_scalar2_3)
coef(mod_scalar2_3, simplify=TRUE)
residuals(mod_scalar2_3)
plot(mod_configural_3)
plot(mod_configural_3, type = 'info')
plot(mod_configural_3, type = 'trace')
plot(mod_configural_3, type = 'trace', which.items = 1:4)
plot(mod_configural_3, type = 'trace', which.items = 5:8)
itemplot(mod_configural_3, 2)
itemplot(mod_configural_3, 2, type = 'RE')


